package com.qx.chat.system.event;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.query.Update;

import com.qx.chat.system.dao.AbstractRepository;
import com.qx.chat.system.dao.BandDaoImpl;
import com.qx.chat.system.dao.DeptUserDaoImpl;
import com.qx.chat.system.dao.UserDaoImpl;
import com.qx.chat.system.entity.Band;
import com.qx.chat.system.entity.DeptUser;
import com.qx.chat.system.entity.User;
import com.qx.chat.system.util.SpringUtils;
import com.qx.chat.websocket.interceptor.QxWebSocketHandshakeInterceptor;


/**
 * 分组自主行为相关事件
 * 
 * @author cyf
 * 
 */
public class BandEvent {

	/**
	 * 编辑分组
	 * 
	 * @author cyf
	 * 
	 */
	public static class BandEditEvent extends Event implements Serializable {

		private static final long serialVersionUID = -7430185558396236914L;
		private static final Logger LOGGER = LoggerFactory.getLogger(QxWebSocketHandshakeInterceptor.class);
		public final static String EDITTYPE = "BandEdit";
		public final static String ADDMEMBERTYPE = "BandAddMember";
		public final static String DELTYPE = "BandDel";
		public final static String DELMEMBERTYPE = "BandDelMember";
		private String loginName;
		private String bandId;;
		private String bandName;
		private String memberLoginName;
		private String errorMsg;
		public BandEditEvent(String loginName,String bandId,String bandName,String memberLoginName) {
			super();
			this.loginName = loginName;
			this.bandId = bandId;
			this.bandName = bandName;
			this.memberLoginName = memberLoginName;
		}
		//新增或编辑分组
		public void createOrUpdate(){
			super.setType(EDITTYPE);
			AbstractRepository<User> userService = SpringUtils.getBean(UserDaoImpl.class);
			User user = (User) userService.findOne("loginName", loginName);
			if(user!=null){
				AbstractRepository<Band> bandService = SpringUtils.getBean(BandDaoImpl.class);
				Band band = bandService.findById(bandId);
				if(band == null){
					AbstractRepository<DeptUser> deptUserService = SpringUtils.getBean(DeptUserDaoImpl.class);
					DeptUser du = (DeptUser) deptUserService.findOne("user", user.getId());
					band = new Band();
					band.setName(bandName);
					band.setOrder(1);
					band.setOwner(user);
					band.initData(user, du.getDept());
					bandService.insert(band);
					LOGGER.info(user.getNickname()+"创建分组："+bandName);
				}else{
					bandService.findAndModify(band.getId(), new Update().set("name", bandName));
				}
			}else{
				this.errorMsg = "分组不存在";
			}
		}
		//分组添加成员事件
		public void addMember(){
			super.setType(ADDMEMBERTYPE);
			AbstractRepository<Band> bandService = SpringUtils.getBean(BandDaoImpl.class);
			Band band = bandService.findById(bandId);
			if(band != null){
				AbstractRepository<User> userService = SpringUtils.getBean(UserDaoImpl.class);
				User member = (User) userService.findOne("loginName", memberLoginName);
				List<User> members = band.getMembers();
				if(members==null){
					members = new ArrayList<User>();
				}
				members.add(member);
				bandService.findAndModify(band.getId(), new Update().set("members", members));
				LOGGER.info("分组添加成员："+member.getNickname());
			}else{
				this.errorMsg = "分组不存在";
			}
		}
		//分组删除事件
		public void delBand(){
			super.setType(DELTYPE);
			AbstractRepository<Band> bandService = SpringUtils.getBean(BandDaoImpl.class);
			try{
				bandService.removeById(bandId);
				LOGGER.info("删除分组："+bandId);
			}catch(Exception e){
				this.errorMsg = "分组不存在";
			}
		}
		//分组删除成员事件
		public void delMember(){
			super.setType(DELMEMBERTYPE);
			AbstractRepository<Band> bandService = SpringUtils.getBean(BandDaoImpl.class);
			Band band = bandService.findById(bandId);
			if(band != null){
				AbstractRepository<User> userService = SpringUtils.getBean(UserDaoImpl.class);
				User member = (User) userService.findOne("loginName", memberLoginName);
				List<User> members = band.getMembers();
				if(members!=null && members.size()>0){
					for (User user : members) {
						if(user.getId().equals(member.getId())){
							members.remove(user);
							break;
						}
					}
				}
				bandService.findAndModify(band.getId(), new Update().set("members", members));
				LOGGER.info("删除分组成员："+member.getNickname());
			}else{
				this.errorMsg = "分组不存在";
			}
		}
		public String getLoginName() {
			return loginName;
		}
		public void setLoginName(String loginName) {
			this.loginName = loginName;
		}
		public String getBandId() {
			return bandId;
		}
		public void setBandId(String bandId) {
			this.bandId = bandId;
		}
		public String getBandName() {
			return bandName;
		}
		public void setBandName(String bandName) {
			this.bandName = bandName;
		}
		public String getErrorMsg() {
			return errorMsg;
		}
		public void setErrorMsg(String errorMsg) {
			this.errorMsg = errorMsg;
		}
	}
}
